package com.annhoa.system.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.annhoa.core.base.BasePage;
import com.annhoa.core.web.BaseController;
import com.annhoa.system.domain.User;
import com.annhoa.system.service.UserService;
import com.annhoa.util.PageData;

/**
 * 
 * @author admin
 * @RestController:@ResponseBody和@Controller的合集
 * 
 */
@RestController
@RequestMapping(value = "/users")
public class UserController extends BaseController{
	
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/add/{id}/{name}/{addressId}/{age}")
	public User addUser(@PathVariable int id, @PathVariable String name, @PathVariable int addressId,
			@PathVariable int age) {
		User user = new User();
		user.setId(id);
		user.setName(name);
		user.setAddressId(addressId);
		user.setAge(age);
		userService.saveUser(user);
		return user;
	}

	@RequestMapping(value = "/delete/{id}")
	public void deleteBook(@PathVariable int id) {
		userService.delete(id);
	}

	@RequestMapping(value = "/")
	public List<User> getUsers() {
		return userService.findAll();
	}
	//添加登录拦截
	@RequestMapping(value = "/findByParams")
	public List<PageData> findByParams() throws Exception {
		PageData pd = this.getPageData();
//		pd.put("id", "2");
//	    return userService.findForObject(pd);
	    return userService.findForList(pd);
	}
	@RequestMapping(value = "/findUser")
	public List<User> findUser() throws Exception {
		return userService.findForList(new User());
	}
	//分页
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/pageList")
	public Page<Map> pageList(BasePage page) throws Exception {
		
		if (page==null) {
			page = new BasePage();
		}
//		Sort sortx = Sort.by(Sort.Direction.DESC,"age"); 没什么卵用 sql 中添加；
//		page.setPr(PageRequest.of(1, 5, sortx));
		return userService.pageList(page);
	}
	
	@RequestMapping(value = "/save")
	public void save() throws Exception {
		PageData pd = new PageData();
		pd.put("id", 4);
		pd.put("name", "溜溜溜");
		pd.put("address_id", 3);
		pd.put("age", 35);
	   userService.save(pd);
	}

	@RequestMapping(value = "/{id}")
	public User getUser(@PathVariable int id) {
		User user = (User) userService.findById(id);
		return user;
	}
	
	@RequestMapping(value = "/search/name/{name}")
	public List<User> getUserByName(@PathVariable String name) {
		List<User> users = userService.findByName(name);
		return users;
	}
	
	/**
	 * @Query注解方式查询,
	 * 用@Param指定参数，匹配age
	 */
	@SuppressWarnings("unused")
	@RequestMapping("/findByName")
	public void findByName(){
		 //按照ID倒序排列
		  System.out.println("直接创建sort对象，通过排序方法和属性名");
		  Sort sort = new Sort(Sort.Direction.DESC,"id");
		  List<User> result = userService.findByName("张三",sort);
		  for (User user:result){
		    System.out.println(user.toString());
		  }
		  System.out.println("-------------------------------------------");
		  //按照ID倒序排列
		  System.out.println("通过Sort.Order对象创建sort对象");
		  //Sort sortx = new Sort(new Sort.Order(Sort.Direction.DESC,"id"));
		  Sort sortx = Sort.by(Sort.Direction.DESC,"id");
		  List<User> resultx = userService.findByName("张三",sort);
		  for (User user:result){
		    System.out.println(user.toString());
		  }
		  System.out.println("-------------------------------------------");
		 
		  System.out.println("通过排序方法和属性List创建sort对象");
		  List<String> sortProperties = new ArrayList<>();
		  sortProperties.add("id");
		  sortProperties.add("age");
		  Sort sort2 = new Sort(Sort.Direction.DESC,sortProperties);
		  List<User> result2 = userService.findByName("张三",sort2);
		  for (User user:result2){
		    System.out.println(user.toString());
		  }
		  System.out.println("-------------------------------------------");
		 
		  System.out.println("通过创建Sort.Order对象的集合创建sort对象");
		  List<Sort.Order> orders = new ArrayList<>();
//		  orders.add(new Sort.Order(Sort.Direction.ASC,"age"));
//		  orders.add(new Sort.Order(Sort.Direction.DESC,"id"));
//		  List<User> result3 = userService.findByName("张三",new Sort(orders));
		  orders.add(new Sort.Order(Sort.Direction.ASC,"age"));
		  orders.add(new Sort.Order(Sort.Direction.DESC,"id"));
		  List<User> result3 = userService.findByName("张三",Sort.by(orders));
		  for (User user:result3){
		    System.out.println(user.toString());
		  }
		  System.out.println("-------------------------------------------");
	}
}